package org.yang.snake.config;

import lombok.SneakyThrows;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.yang.snake.config.ClassPathTldsLoader;

import javax.annotation.Resource;

/**
 * @author shichenyang
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder getEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 用户签名,验证用户密码是否正确,密码编码器
     * @param auth 用户签名管理器,使用userDetails的方式来完成用户签名
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(getEncoder());
    }

    @SneakyThrows
    @Override
    public void configure(WebSecurity web){
        //过滤的请求地址，不需要进行拦截
        web
                .ignoring()
                .antMatchers("/layui/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //所有验证后的请求
                .authorizeRequests()
                //必须验证后才能访问
                .anyRequest().authenticated()
                .and()
                //添加登录页面
                .formLogin()
                //表单登录页面地址
                .loginPage("/login.html")
                //登录成功后跳转的页面
                .defaultSuccessUrl("/")
                //所有人都可以访问
                .permitAll()
                .and()
                //增加退出
                .logout()
                //退出成功后去往的页面
                .logoutSuccessUrl("/login.html")
                //所有人都可以操作
                .permitAll()
                .and()
                //记住我功能
                .rememberMe().tokenValiditySeconds(86400)
                //记住我的名称
                .key("remember-token")
                .and()
                //去掉csrf验证
                .csrf().disable()
                .headers().frameOptions().disable();
    }
    /**
     * 自动加载security-taglibs
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(ClassPathTldsLoader.class)
    public ClassPathTldsLoader classPathTldsLoader(){
        return new ClassPathTldsLoader("");
    }
}
